import os
import uuid
#  datatime 把事件转化成时间戳  timedelta 时间间隔
from datetime import datetime,timedelta

from flask import Blueprint, make_response, redirect, jsonify
from flask import request, render_template
from werkzeug.datastructures import FileStorage

import settings
from models.user import User,db
from utils import crypt, cache

blue = Blueprint('userBlue', __name__)


@blue.route('/modify', methods=['GET','POST'])
def modify():
    token = request.cookies.get('token')
    user_id = cache.get_user_id(token)
    # 优化登录用户的相关信息存在redis中

    user = User.query.get(int(user_id))
    msg = ''
    if request.method == 'POST':
        # 头像上传
        # 获取上传的文件，
        # user_photo必须和前端请求中文件字段名保持一致
        # 冒号，后面跟个类，表示当前类的对象是指这个类的对象
        upload_file:FileStorage = request.files.get('user_photo')
        print('文件名:',upload_file.filename)
        #print('文件长度（字节）:', upload_file.content_length)
        print('文件类型:', upload_file.content_type)
        # 验证文件是否为图片
        if not upload_file.content_type.startswith('image/'):
            msg = '只支持图片上传'
        else:
            filename = uuid.uuid4().hex + os.path.splitext(upload_file.filename)[-1]
            filepath = os.path.join(settings.USER_DIR,filename)

            # 服务端保存上传的文件
            upload_file.save(filepath)

            # 更新用户信息
            # 保存在数据库的图片是相对static资源访问路径
            user.photo = 'user/' + filename
            db.session.commit()

    return render_template('user/info.html',user=user,msg=msg)


@blue.route('/upload', methods=['POST'])
def upload_photo():
    # 拿到的图片类型为Filestorage
    upload_file: FileStorage = request.files.get('photo')

    filename = uuid.uuid4().hex+os.path.splitext(upload_file.filename)[-1]
    filepath = os.path.join(settings.USER_DIR,filename)

    upload_file.save(filepath)

    user = User.query.get(cache.get_user_id(request.cookies.get('token')))
    user.photo = 'user/'+filename
    db.session.commit()

    # 1 、test能否访问接口
    return jsonify({
        'result':'上传成功',
        'path':'/user/'+filename
    })


@blue.route('/logout',methods=['GET'])
def loginout():
    token = request.cookies.get('token')
    # 删除redis的token
    cache.clear_token(token)
    # 返回resp对象

    resp = redirect('/user/login')
    # 删除cookie中的token
    resp.delete_cookie('token')

    return resp

@blue.route('/login', methods=['GET', 'POST'])
def login():
    message= ''
    if request.method == 'POST':
        phone = request.form.get('phone')
        passwd = request.form.get('passwd')

        login_user = User.query.filter(User.phone==phone,
                                       User.auth_key==crypt.pwd(passwd)).one()
        if login_user:
            # 账户密码验证成功
            # 生成token
            token = uuid.uuid4().hex
            resp = redirect('/')
            resp.set_cookie('token', token,
                            expires=(datetime.now()+timedelta(days=3)))
            # 将token添加到redis，token-user_id
            cache.save_token(token,login_user.id)
            return resp
        else:
            message = '查无此用户'
    return render_template('user/login.html',msg=message)


if __name__ == '__main__':
    upload_photo()